package com.supor.aiot.home.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * ---------------------------------------------------------------- <br>
 * Copyright (C) 2014-2021, by het, ShenZhen, All rights reserved.  <br>
 * ---------------------------------------------------------------- <br>
 * <p>
 * 描述: TwinkleTextView <br>
 * 作者: lei <br>
 * 日期: 2021/9/26 <br>
 */
public class TwinkleTextView extends TextView implements Runnable {

    /**
     * 是否显示
     */
    private boolean flag = true;
    /**
     * 闪动内容
     */
    private String twinkleText = ":";

    /**
     * 闪动时间
     */
    private int delayMillis = 500;


    private boolean run = false;

    public TwinkleTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TwinkleTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public TwinkleTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public void beginRun() {
        run = true;
        run();
    }

    @Override
    public void run() {
        if (run) {
            if (flag && run) {
                setVisibility(VISIBLE);
                flag = false;
            } else {
                setVisibility(INVISIBLE);
                flag = true;
            }
            postDelayed(this, delayMillis);
        }
    }

    public String getTwinkleText() {
        return twinkleText;
    }

    public void setTwinkleText(String twinkleText) {
        this.twinkleText = twinkleText;
        setText(twinkleText);
    }

    public int getDelayMillis() {
        return delayMillis;
    }

    public void setDelayMillis(int delayMillis) {
        this.delayMillis = delayMillis;
    }

    public boolean isRun() {
        return run;
    }

    public void setRun(boolean run) {
        this.run = run;
    }

}
